*********************************************************************************************************
*This file cleans data by the replaceability to robots of workers by education level from the Census/ACS*
*********************************************************************************************************

foreach year in 1990 2008 {

use if year==`year' using "$raw_data_lmarket/ipums_census.dta", clear

*Drop institutional group quarters* 
quietly: drop if gqtyped>=100 & gqtyped<=499
*Drop alaska and hawai*
quietly: drop if statefip==2 | statefip==15

keep if age>=19&age<=64

*Keep only those individuals for which we have information on the occupation
keep if (occ1990>=4 & occ1990<=900)

keep cntygp* puma statefip perwt multyear empstat classwkrd ind1990 occ1990 age educd school hispan sex bpl degfield degfieldd degfield2 degfield2d

*Graetz and Michaels xwalk
merge m:1 occ1990 using "$contributed_data/replaceability_occ", keep(1 3) nogenerate

*employment
gen ipums_emp = 1

*replaceable
gen ipums_rep = (replaceable==1)

*education level
foreach var of varlist ipums_* {

	gen `var'_b = (`var'==1 & educd>=100 & educd<=116)
	gen `var'_a = (`var'==1 & educd>=81 & educd<=83)
	gen `var'_l = (`var'==1 & educd>=2 & educd<=71)

}

keep cntygp* puma statefip ipums_* perwt multyear

**Merge czones using geography xwalk**
if `year'==1990{
gen puma1990=statefip*10000+puma
collapse (sum) ipums_* [fw=perwt], by(puma1990)  fast
count if puma1990!=.
joinby puma1990 using "$project/xwalks/xwalks_geography/puma1990_czone.dta", unmatched(master)
assert czone!=. 
}
else if `year'==2008 {
replace puma=1801 if puma==77777
gen puma2000=statefip*10000+puma
collapse (sum) ipums_* [fw=perwt], by(puma2000)  fast
count if puma2000!=.
joinby puma2000 using "$project/xwalks/xwalks_geography/puma2000_czone.dta", unmatched(master)
assert czone!=. 
}

**Aggregate at the czone level**
collapse (sum) ipums_* [iw=afac], by(czone) fast
foreach var of varlist ipums_*{
rename `var' `var'_`year'
}
save "$clean_data_lmarket/czone`year'_educationReplaceability.dta", replace
}
